package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.pojo.SetmealDish;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 套餐管理
 */
@RestController
@RequestMapping("setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private DishService dishService;

    //分页查询
    @GetMapping("page")
    public R selectByPage(Integer page, Integer pageSize, String name) {
        Page<SetmealDto> page1 = setmealService.pageSetmealDto(page, pageSize, name);
        return R.success(page1);
    }

    //添加
    @PostMapping()
    @CacheEvict(cacheNames = "setmeal",allEntries = true) //移除所有setmeal开头的缓存
    public R add(@RequestBody SetmealDto setmealDto) {
        setmealService.addSetMealDto(setmealDto);
        return R.success("添加成功");
    }

    //批量删除
    @DeleteMapping()
    @CacheEvict(cacheNames = "setmeal",allEntries = true) //移除所有setmeal开头的缓存
    public R delete(@RequestParam List<Long> ids) {
        setmealService.deleteByIds(ids);
        return R.success("删除成功");
    }

    //批量启停售
    @PostMapping("status/{status}")
    @CacheEvict(cacheNames = "setmeal",allEntries = true) //移除所有setmeal开头的缓存
    public R updateByStatus(@PathVariable Integer status, Long[] ids) {
        setmealService.lambdaUpdate()
                .in(Setmeal::getId, ids)
                .set(Setmeal::getStatus, status)
                .update();
        return R.success("修改成功");
    }

    //根据id查询回显数据
    @GetMapping("{id}")
    public R selectById(@PathVariable Long id) {
        SetmealDto setmealDto = setmealService.selectWithDish(id);
        return R.success(setmealDto);
    }

    //修改套餐信息
    @PutMapping()
    @CacheEvict(cacheNames = "setmeal",allEntries = true) //移除所有setmeal开头的缓存
    public R update(@RequestBody SetmealDto setmealDto) {
        setmealService.updateWithDish(setmealDto);
        return R.success("修改成功");
    }

    //移动端展示套餐
    @GetMapping("list")
    @Cacheable(cacheNames = "setmeal",key = "#categoryId+'_'+#status",unless = "#result.data.size()<=0")
    public R list(Long categoryId, Integer status) {
        List<Setmeal> list = setmealService.lambdaQuery()
                .eq(Setmeal::getCategoryId, categoryId)
                .eq(Setmeal::getStatus, status)
                .list();
        return R.success(list);
    }

    //移动端单击展示菜品详细数据
    @GetMapping("dish/{setmealId}")
    public R dish(@PathVariable Long setmealId) {
        List<DishDto> list2 = dishService.getDishBysetmealId(setmealId);
        return R.success(list2);
    }
}
